const socketWrapper = require('./index');
const { redisGet } = require('../utils/redis')
const { getOnlineServiceList } = require('../controller/user')

function onlineService() {
    const io = socketWrapper.getIO();
    io.on('connection', async (socket) => {
        socket.on('onlineService', async (data, callback) => {
            try {
                const onlineServiceList = await getOnlineServiceList();
                console.log('onlineServiceList', onlineServiceList)
                if (onlineServiceList.length > 0) {
                    // 获取返会中字段workNum最少的一个
                    const findOneService = onlineServiceList.sort((a, b) => a.workNum - b.workNum)[0];
                    callback({
                        code: 200,
                        message: '获取在线客服成功',
                        data: findOneService
                    });

                } else {
                    callback({
                        code: 500,
                        message: '当前暂无客服在线，客服值班时间为9:00-21:00。请在客服工作时间内再咨询。'
                    });
                }
            } catch (e) {
                callback({
                    code: 500,
                    message: '获取在线客服列表失败'
                });
            }
        });
    });
}

module.exports = onlineService;
